package com.yx.louti.interceptor;

import com.yx.louti.globalException.enums.HttpResultCode;
import com.yx.louti.globalException.model.BizException;
import com.yx.louti.web.bean.User;
import jtool.JString;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 登录拦截器
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {
    //token失效时间
    public static final long token_valid_seconds = 24 * 3600000;


    //不需要更新redis的过期时间, 过期之后大不了重新再从数据库查一次
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //token判断
        String token = request.getHeader("token");
        if (JString.isEmpty(token)) {
            throw new BizException(HttpResultCode.USER_NOTLOGGED_IN);
        }
        HttpSession session = request.getSession();
        //这里的User是登陆时放入session的
        User user = (User) session.getAttribute("user");
        if(null == user){
            throw new BizException(HttpResultCode.USER_NOTLOGGED_IN);
        }
        return true;

    }

}
